package com.oocl.servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.constant.Constant;


@WebServlet("/sec/image/*")
public class ImageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String cpath = request.getRequestURI();
		int n = cpath.indexOf("image")+5; 
		String path = cpath.substring(n);
		byte[] buf = getImageByte(Constant.IMAGE_DIR+path);
		OutputStream out = response.getOutputStream();
		out.write(buf);
		out.flush();
		out.close();
	}

	private byte[] getImageByte(String path) throws IOException {
		InputStream in = new FileInputStream(path);
		byte[] buf = new byte[in.available()];
		in.read(buf);
		in.close();
		return buf;
	}

}
